/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.statemachine.editpart.TransitionEditPart;
import com.cea.papyrus.diagram.statemachine.editpart.TransitionGuardAndAllDisplayEditPart;
import com.cea.papyrus.diagram.statemachine.helper.EdgeTransitionHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Manages guard section of Transitions.
 *
 * @author David Servat
 * @since 10 apr. 07
 */
public class TransitionGuardSection extends PropertyViewSection{

	/**
	 * The Class DirectEditFocusListener.
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener{

		/**
		 * Focus gained.
		 * 
		 * @param e the e
		 */
		public void focusGained(FocusEvent e){}

		/**
		 * Focus lost.
		 * 
		 * @param e the e
		 */
		public void focusLost(FocusEvent e){
			if(!guardText.isDisposed()){
				if((umlElement != null) && (umlElement instanceof Transition)){			
					if(!guardText.getText().trim().equals("")){
						String transitionGuard = "[ " + guardText.getText().trim() + " ]";
						LiteralString ls = UMLFactory.eINSTANCE.createLiteralString();
						ls.setValue(transitionGuard);

						//test the existence of a guard
						Transition transition = (Transition)umlElement;
						Constraint guard = transition.getGuard();
						if(guard == null){
							guard = UMLFactory.eINSTANCE.createConstraint();
							guard.setName("Guard");
						}
						guard.setSpecification(ls);
						transition.setGuard(guard);
					}
					else{
						//test the existence of a guard
						Transition transition = (Transition)umlElement;
						Constraint guard = transition.getGuard();

						if(guard != null){
							transition.setGuard(null);
							transition.getOwnedRules().remove(guard);
						}
					}
				}
			}
		}
	}

	/**
	 * The guard text.
	 */
	private Text guardText;

	/**
	 * The direct edit focus listener.
	 */
	private DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);

		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		guardText = this.getWidgetFactory().createText(composite, "");
		guardText.setEditable(true);
		// nameText.addFocusListener(directEditFocusListener);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		guardText.setLayoutData(data);

		guardText.addFocusListener(directEditFocusListener);

		CLabel nameLabel = this.getWidgetFactory().createCLabel(composite, "Guard:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(guardText, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(guardText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if (!guardText.isDisposed()){
			if(umlElement instanceof Transition){
				guardText.removeFocusListener(directEditFocusListener);
				if(((Transition)umlElement).getGuard() != null){
					Constraint guard = ((Transition)umlElement).getGuard();
					if(guard.getSpecification() instanceof LiteralString){
						LiteralString ls = (LiteralString)guard.getSpecification();
						String guardString = ls.stringValue();
						guardText.setText(guardString.substring(guardString.indexOf("[") + 1, guardString.lastIndexOf("]")).trim());
						if(graphicalElement != null){
							if(graphicalElement instanceof TransitionEditPart){
								TransitionGuardAndAllDisplayEditPart guardAndAll = ((TransitionEditPart)graphicalElement).getGuardAndAllDisplayEditPart();
								GraphNode gN = guardAndAll.getGraphNode();
								if((gN != null) && (gN.getProperty(EdgeTransitionHelper.TRANSITION_DISPLAY_GUARD) != null))
									gN.getProperty(EdgeTransitionHelper.TRANSITION_DISPLAY_GUARD).setValue(EdgeTransitionHelper.TRANSITION_DISPLAY);
							}
						}
					}
					else{
						guardText.setText("");
					}
				}
				else{
					guardText.setText("");
				}
				guardText.addFocusListener(directEditFocusListener);
			}				
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if(guardText!=null && !guardText.isDisposed())
			guardText.removeFocusListener(directEditFocusListener);
	}
}